package com.baor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baor.dao.SeparateConfDao;
import com.baor.domain.GoodsVo;
import com.baor.domain.conf.SeparateConf;
import com.baor.service.SeparateConfService;
import com.baor.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SeparateConfServiceImpl implements SeparateConfService {

    @Autowired
    private SeparateConfDao separateConfDao;

    @Override
    public AjaxResult<?> separateConfInfo(SeparateConf separateConf) {
        LambdaQueryWrapper<SeparateConf> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SeparateConf::getSiteId, separateConf.getSiteId());
        return AjaxResult.success(separateConfDao.selectList(queryWrapper));
    }


    @Override
    public AjaxResult<?> updateSeparateConf(SeparateConf separateConf) {
        LambdaQueryWrapper<SeparateConf> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SeparateConf::getConfLevel, separateConf.getConfLevel());
        List<SeparateConf> list = separateConfDao.selectList(queryWrapper);
        long count = list.stream().filter(x -> x.getSiteId().equals(separateConf.getSiteId())).count();
        if ( count > 0 ){
            LambdaQueryWrapper<SeparateConf> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(SeparateConf::getConfLevel, separateConf.getConfLevel()).eq(SeparateConf::getSiteId, separateConf.getSiteId());
            int row = separateConfDao.update(separateConf, queryWrapper2);
            if (row > 0) {
                return AjaxResult.success("更新成功");
            }else {
                return AjaxResult.error("更新失败");
            }
        }else {
            int row = separateConfDao.insert(separateConf);
            if (row > 0) {
                return AjaxResult.success("新增成功");
            }else {
                return AjaxResult.error("新增失败");
            }
        }
    }
}
